<?php
/**
 * Here is your custom functions.
 */

/**
 * 当前登录管理员id
 *
 * @return mixed|null
 */
function admin_id()
{
    return session('admin.id');
}

/**
 * 当前管理员
 *
 * @param null|array|string $fields
 * @return array|mixed|null
 */
function admin($fields = null)
{
    if (!$admin = session('admin')) {
        return null;
    }
    if ($fields === null) {
        return $admin;
    }
    if (is_array($fields)) {
        $results = [];
        foreach ($fields as $field) {
            $results[$field] = $admin[$field] ?? null;
        }
        return $results;
    }
    return $admin[$fields] ?? null;
}

/**
 * 当前登录用户id
 *
 * @return mixed|null
 */
function user_id()
{
    return session('user.id');
}

/**
 * 当前登录用户
 *
 * @param null|array|string $fields
 * @return array|mixed|null
 */
function user($fields = null)
{
    if (!$user = session('user')) {
        return null;
    }
    if ($fields === null) {
        return $user;
    }
    if (is_array($fields)) {
        $results = [];
        foreach ($fields as $field) {
            $results[$field] = $user[$field] ?? null;
        }
        return $results;
    }
    return $user[$fields] ?? null;
}

/**
 * 获取当前路径，并转换为前端访问路径
 * 约定大于
 */
function get_curent_path( $type = 'url'){
    $path = request()->controller;  // plugin\admin\app\controller\slider\Slider
    $targetCode = ['plugin','app\controller\\','\\'];
    $transCode = ['','','/'];
    $str = strtolower(str_replace($targetCode, $transCode, $path));
    if($type == 'api'){
        $str = '/app' . $str;
    }
    return $str;
}
function abort($code = 1001, string $message = ''){
    throw new \support\exception\BusinessException($message,$code);
}

function logChannel($channel = 'infoLog'){
    if(!str_contains($channel,'.')){
        $channel = "plugin.admin.$channel";
    }
    return support\Log::channel($channel);
}